package com.javasm.dao.impl;

import com.javasm.dao.LoginDao;
import com.javasm.entity.AdminMenu;
import com.javasm.entity.AdminUser;
import com.javasm.entity.sql.LoginManageSql;
import com.javasm.util.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author WangJie
 * @version 1.0
 * @Description:
 * @date: 2020/7/15 15:21
 * @since JDK 1.8
 */
public class LoginDaoImpl implements LoginDao {
    /**
     * 判断登录
     * @param name 登录用户名
     * @return 用户信息
     */
    @Override
    public AdminUser isExist(String name) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        AdminUser adminUser = null;
        try {
            adminUser = queryRunner.query(LoginManageSql.SELECT_ADMIN_BY_ANAME, new BeanHandler<>(AdminUser.class), name);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
//            DruidUtil.close();
        }
        return adminUser;
    }

    /**
     * 更新最近登录时间
     * @param name 登录用户名
     */
    @Override
    public void updateLoginTime(String name) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        try {
            queryRunner.update(LoginManageSql.UPDATE_LOGINTIME_BY_ANAME,name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改密码
     *
     * @param aid     要修改账户的id
     * @param newPass 要修改账户的新密码
     * @return
     */
    @Override
    public int alterPassword(String aid, String newPass) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        int result = 0;
        try {
            result = queryRunner.update(LoginManageSql.UPDATE_APWD_BY_AID, newPass, aid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 分页查询账户列表
     *
     * @param page     当前页码
     * @param pageSize 每页查询条数
     * @return
     */
    @Override
    public List<AdminUser> selectAdminByPage(int page, int pageSize) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        List<AdminUser> adminUserList = null;
        try {
            adminUserList = queryRunner.query(LoginManageSql.SELECT_ADMIN_BY_PAGE, new BeanListHandler<>(AdminUser.class), (page-1)*pageSize, pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return adminUserList;
    }

    /**
     * 查询用户信息总记录数
     * @return
     */
    @Override
    public int selectTotalCount() {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        int totalCount = 0;
        try {
            totalCount = ((Long)queryRunner.query(LoginManageSql.SELECT_ADMIN_TOTALCOUNT, new ScalarHandler<>())).intValue();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return totalCount;
    }
}
